<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Express extends Admin_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see https://codeigniter.com/user_guide/general/urls.html
	 */
	public function __construct() {
		parent::__construct();
		$this->load->model('Express_model', 'expModel');
	}

	public function index()
	{
		$data=$this->data;
		$this->load->view('admin/Express/index',$data);
	}
	//页面渲染
	public function express_list()
	{
		$name=$this->input->get('name');
		$limit = $this->input->get('limit');
		$page = $this->input->get('page')==1?0:($this->input->get('page')-1)*$limit;
		$list=$this->expModel->getExpressList($limit,$page,$name);
		$result['code'] = 0;
		$result['msg'] = '';
		$result['data'] = $list;
		$result['count'] = $this->expModel->getExpressCount();
		echo json_encode($result);
	}

	//修改是否启用
	public function upEnable()
	{
		$id = intval($this->input->post('id'));
		$sign = $this->input->post('sign');
		switch ($sign) {
			case '1':
				$this->expModel->updateExpress($id,['enable'=>2]);
				echo json_encode(array('msg' => 1));
				break;
			case '0':
				$this->expModel->updateExpress($id,['enable'=>1]);
				echo json_encode(array('msg' => 1));
				break;
		}
	}
	public function express_add()
	{
		if($this->input->is_ajax_request())
		{
			$name=$this->input->post('data')['name'];
			$type=$this->input->post('data')['type'];
			$inx=intval($this->input->post('data')['inx']);
			if(!($name&&$type&&$inx))
			{
				die(json_encode(['code'=>0,'content'=>'缺少参数']));
			}
			$res=$this->expModel->insertExpress(['name'=>$name,'type'=>$type,'inx'=>$inx]);
			if($res) die(json_encode(['code'=>1,'content'=>'添加成功']));
			else die(json_encode(['code'=>0,'content'=>'添加失败']));
		}
		else
		{
			$this->load->view('admin/Express/add');
		}
	}
	public function express_edit()
	{
		if($this->input->is_ajax_request())
		{
			$name=$this->input->post('data')['name'];
			$type=$this->input->post('data')['type'];
			$inx=intval($this->input->post('data')['inx']);
			if(!($name&&$type&&$inx))
			{
				die(json_encode(['code'=>0,'content'=>'缺少参数']));
			}
			$res=$this->expModel->updateExpress(intval($this->input->post('data')['id']),['name'=>$name,'type'=>$type,'inx'=>$inx]);
			if($res) die(json_encode(['code'=>1,'content'=>'编辑成功']));
			else die(json_encode(['code'=>0,'content'=>'编辑失败']));
		}
		else
		{
			$data=$this->expModel->getExpress($this->input->get('id'));
			$this->load->view('admin/Express/edit',$data);
		}
	}
	public function express_del()
	{
		$id=intval($this->input->post('id'));
		$res=$this->expModel->delExpress(intval($this->input->post('id')));
		if($res) die(json_encode(['code'=>1,'content'=>'删除成功']));
		else die(json_encode(['code'=>0,'content'=>'删除失败']));
	}
	public function batchOper()
	{
		$ids=$this->input->post('id');
		$enable=$this->input->post('enable');
		if(count($ids)==count($enable))
		{
			for($i=0;$i<count($enable);$i++)
			{
				$val=$enable[$i]==1?2:1;
				$this->expModel->updateExpress(intval($ids[$i]),['enable'=>$val]);
			}
			echo die(json_encode(['code'=>1]));
		}
		echo die(json_encode(['code'=>0]));
	}
}
